﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Xml;
using Command.Model;
using System.Reflection;

namespace NJGZBTYHXT
{
    public partial class Form4 : Form
    {
        XmlDocument xmlDoc;
        string filename;
        XmlNode node;
        private DataGridView _dgv;

        public Form4()
        {
            InitializeComponent();
        }

        private void LoadColumn()
        {
            try
            {
                comboBox1.Items.Clear();
                foreach (DataGridViewColumn dgvc in _dgv.Columns)
                {
                    comboBox1.Items.Add(dgvc.Name);
                }
                if (comboBox1.Items.Count > 0) { comboBox1.SelectedIndex = 0; }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Exception");
            }
        }

        private void LoadData()
        {
            try
            {
                XmlNodeList list = xmlDoc.SelectNodes("/Alternate/item");

                IList<HX> hxList = new List<HX>();

                foreach (XmlNode xn in list)
                {
                    HX hx = new HX();

                    hx.Id = xn.Attributes["id"].Value;
                    hx.End = int.Parse(xn.Attributes["end"].Value);
                    hx.Range = bool.Parse(xn.Attributes["range"].Value);
                    hx.Column = xn.Attributes["column"].Value;
                    hx.Start = int.Parse(xn.Attributes["end"].Value);
                    hx.Value = xn.Attributes["value"].Value;

                    hxList.Add(hx);
                }
                dataGridView1.DataSource = hxList;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Exception", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void LD()
        {
            if (node != null)
            {
                try
                {
                    string OpenAlternate = node.Attributes["open"].Value;

                    checkBox2.Checked = OpenAlternate.ToUpper().Equals("TRUE") ? true : false;

                    CB2();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Exception", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        private void CB2()
        {
            groupBox2.Enabled = groupBox1.Enabled = checkBox2.Checked;
            checkBox2.Text = "状态：" + checkBox2.Checked;
        }

        private void checkBox2_CheckedChanged(object sender, EventArgs e)
        {
            CB2();
        }

        private void label3_Click(object sender, EventArgs e)
        {

        }

        private void button3_Click(object sender, EventArgs e)
        {
            try
            {
                if (checkBox2.Checked)
                {
                    node.Attributes["open"].Value = "true";
                }
                else
                {
                    node.Attributes["open"].Value = "false";
                }

                xmlDoc.Save(filename);

                MessageBox.Show("保存完成", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Exception");
            }
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            C();
        }

        private void C()
        {
            textBox1.Text = "";
            comboBox1.Text = "";
            textBox3.Text = "";
            checkBox1.Checked = false;
            textBox4.Text = "0";
            textBox5.Text = "0";
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count == 1)
            {
                try
                {
                    textBox1.Text = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();
                    comboBox1.Text = dataGridView1.SelectedRows[0].Cells[1].Value.ToString();
                    textBox3.Text = dataGridView1.SelectedRows[0].Cells[2].Value.ToString();
                    checkBox1.Checked = bool.Parse(dataGridView1.SelectedRows[0].Cells[3].Value.ToString());
                    textBox4.Text = dataGridView1.SelectedRows[0].Cells[4].Value.ToString();
                    textBox5.Text = dataGridView1.SelectedRows[0].Cells[5].Value.ToString();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Exception", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            else
            {
                MessageBox.Show("请选择一行数据");
            }
        }

        private void toolStripButton3_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count == 1)
            {
                if (MessageBox.Show("确定要删除所选数据吗？", "操作提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    try
                    {
                        node = xmlDoc.SelectSingleNode("/Alternate");
                        XmlNode dn = xmlDoc.SelectSingleNode("/Alternate/item[@id='" + dataGridView1.SelectedRows[0].Cells[0].Value + "']");
                        node.RemoveChild(dn);
                        xmlDoc.Save(filename);
                        LoadData();
                    }
                    catch (XmlException ex)
                    {
                        MessageBox.Show(ex.Message, "XmlException", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }
            else
            {
                MessageBox.Show("请选择一行数据");
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                HX hx = new HX();

                if (comboBox1.Text.Trim() == "") { comboBox1.Focus(); MessageBox.Show("请选择或者输入标识列"); return; }

                hx.Column = comboBox1.Text.Trim();

                hx.Range = checkBox1.Checked;

                if (checkBox1.Checked)
                {
                    if (textBox4.Text.Trim() == "") { textBox4.Focus(); MessageBox.Show("请填写开始值"); return; }

                    if (textBox5.Text.Trim() == "") { textBox5.Focus(); MessageBox.Show("请填写结束值"); return; }

                    hx.Start = int.Parse(textBox4.Text.Trim());

                    hx.End = int.Parse(textBox5.Text.Trim());

                    if (hx.End < hx.Start) { textBox4.Focus(); MessageBox.Show("开始至不能大于结束值"); return; }
                }
                else
                {
                    if (textBox3.Text.Trim() == "") { textBox3.Focus(); MessageBox.Show("值不能为空"); return; }

                    hx.Value = textBox3.Text.Trim();
                }

                bool isEdit = false;

                if (textBox1.Text.Trim() != "")
                {
                    hx.Id = textBox1.Text.Trim();
                    isEdit = true;
                }
                else
                {
                    hx.Id = Guid.NewGuid().ToString();
                }

                if (!isEdit)
                {
                    XmlAttribute xaid = xmlDoc.CreateAttribute("id");
                    xaid.Value = hx.Id;

                    XmlAttribute xacolumn = xmlDoc.CreateAttribute("column");
                    xacolumn.Value = hx.Column;

                    XmlAttribute xavalue = xmlDoc.CreateAttribute("value");
                    xavalue.Value = hx.Value;

                    XmlAttribute xarange = xmlDoc.CreateAttribute("range");
                    xarange.Value = hx.Range.ToString();

                    XmlAttribute xastart = xmlDoc.CreateAttribute("start");
                    xastart.Value = hx.Start.ToString();

                    XmlAttribute xaend = xmlDoc.CreateAttribute("end");
                    xaend.Value = hx.End.ToString();

                    XmlNode xn = xmlDoc.CreateElement("item");
                    xn.Attributes.Append(xacolumn);
                    xn.Attributes.Append(xaend);
                    xn.Attributes.Append(xaid);
                    xn.Attributes.Append(xarange);
                    xn.Attributes.Append(xastart);
                    xn.Attributes.Append(xavalue);

                    node.AppendChild(xn);
                }
                else
                {
                    XmlNode item = xmlDoc.SelectSingleNode("/Alternate/item[@id='" + hx.Id + "']");
                    item.Attributes["column"].Value = hx.Column;
                    item.Attributes["value"].Value = hx.Value;
                    item.Attributes["range"].Value = hx.Range.ToString();
                    item.Attributes["start"].Value = hx.Start.ToString();
                    item.Attributes["end"].Value = hx.End.ToString();
                }

                xmlDoc.Save(filename);

                C();

                LoadData();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Exception", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            C();
        }


        public DataGridView Dgv { get { return _dgv; } set { _dgv = value; } }

        private void Form4_Load(object sender, EventArgs e)
        {
            try
            {
                dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;

                dataGridView1.AutoGenerateColumns = false;

                xmlDoc = new XmlDocument();

                filename = Application.StartupPath + "\\" + System.Configuration.ConfigurationManager.AppSettings["AlternateConfigFile"];

                xmlDoc.Load(filename);

                node = xmlDoc.SelectSingleNode("/Alternate");

                LD();

                LoadColumn();

                LoadData();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Exception", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
}
